package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.entity.WebUserInfo;
import com.javasm.service.impl.WebUserInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/playCustomer/*")
public class WebUserInfoServlet extends BaseServlet {

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String nickname = req.getParameter("nickname");
        String customerImgs = req.getParameter("customerImgs");
        String customerRemark = req.getParameter("customerRemark");
        String ipAddress = req.getParameter("ipAddress");
        String hobbyGame = req.getParameter("hobbyGame");
        String hobbyMusic = req.getParameter("hobbyMusic");

        String lockoutTime = req.getParameter("lockoutTime");

        String customerVipGradeStr = req.getParameter("vipGrade");
        String customerLoadGradeStr = req.getParameter("loadGrade");
        String godStatusStr = req.getParameter("godStatus");
        String fansNumStr = req.getParameter("fansNum");
        String lockoutStatusStr = req.getParameter("lockoutStatus");
        Integer vipGrade = null;
        if (customerVipGradeStr != null && !"".equals(customerVipGradeStr)) {
            vipGrade = Integer.parseInt(customerVipGradeStr);
        }
        Integer loadGrade = null;
        if (customerLoadGradeStr != null && !"".equals(customerLoadGradeStr)) {
            loadGrade = Integer.parseInt(customerLoadGradeStr);
        }
        Integer godStatus = null;
        if (godStatusStr != null && !"".equals(godStatusStr)) {
            godStatus = Integer.parseInt(godStatusStr);
        }
        Integer fansNum = null;
        if (fansNumStr != null && !"".equals(fansNumStr)) {
            fansNum = Integer.parseInt(fansNumStr);
        }
        Integer lockoutStatus = null;
        if (lockoutStatusStr != null && !"".equals(lockoutStatusStr)) {
            lockoutStatus = Integer.parseInt(lockoutStatusStr);
        }

        WebUserInfo inputCustomer = new WebUserInfo(nickname, vipGrade, loadGrade, customerImgs, godStatus, fansNum, ipAddress, customerRemark, hobbyGame, hobbyMusic, lockoutStatus, lockoutTime);
        WebUserInfoServiceImpl playCustomerService = new WebUserInfoServiceImpl();
         int resNum = playCustomerService.addPlyCustomer(inputCustomer);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String customerIdStr = req.getParameter("uid");
        Integer uid = Integer.parseInt(customerIdStr);
        WebUserInfoServiceImpl playCustomerService = new WebUserInfoServiceImpl();
        int resNum = playCustomerService.deletePlyCustomerById(uid);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String customerIdStr = req.getParameter("uid");


        String nickname = req.getParameter("nickname");
        String customerImgs = req.getParameter("customerImgs");
        String customerRemark = req.getParameter("customerRemark");
        String ipAddress = req.getParameter("ipAddress");
        String hobbyGame = req.getParameter("hobbyGame");
        String hobbyMusic = req.getParameter("hobbyMusic");



        String customerVipGradeStr = req.getParameter("vipGrade");
        String customerLoadGradeStr = req.getParameter("loadGrade");
        String godStatusStr = req.getParameter("godStatus");
        String fansNumStr = req.getParameter("fansNum");

        Integer uid = null;
        if (customerIdStr != null && !"".equals(customerIdStr)) {
            uid = Integer.parseInt(customerIdStr);
        }
        Integer vipGrade = null;
        if (customerVipGradeStr != null && !"".equals(customerVipGradeStr)) {
            vipGrade = Integer.parseInt(customerVipGradeStr);
        }
        Integer loadGrade = null;
        if (customerLoadGradeStr != null && !"".equals(customerLoadGradeStr)) {
            loadGrade = Integer.parseInt(customerLoadGradeStr);
        }
        Integer godStatus = null;
        if (godStatusStr != null && !"".equals(godStatusStr)) {
            godStatus = Integer.parseInt(godStatusStr);
        }
        Integer fansNum = null;
        if (fansNumStr != null && !"".equals(fansNumStr)) {
            fansNum = Integer.parseInt(fansNumStr);
        }


        WebUserInfo inputCustomer = new WebUserInfo(nickname, vipGrade, loadGrade, customerImgs, godStatus, fansNum, ipAddress, customerRemark, hobbyGame, hobbyMusic, null   , null);
        inputCustomer.setUid(uid);
        WebUserInfoServiceImpl playCustomerService = new WebUserInfoServiceImpl();
        int resNum = playCustomerService.editPlyCustomer(inputCustomer);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
    public void editLockout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String lockoutTime = req.getParameter("lockoutTime");
        String lockoutStatusStr = req.getParameter("lockoutStatus");
        Integer lockoutStatus = null;
        if (lockoutStatusStr != null && !"".equals(lockoutStatusStr)) {
            lockoutStatus = Integer.parseInt(lockoutStatusStr);
        }
        String customerIdStr = req.getParameter("uid");
        Integer uid = null;
        if (customerIdStr != null && !"".equals(customerIdStr)) {
            uid = Integer.parseInt(customerIdStr);
        }
        WebUserInfo webUserInfo = new WebUserInfo(uid, lockoutStatus, lockoutTime);
        WebUserInfoServiceImpl playCustomerService = new WebUserInfoServiceImpl();
        int resNum = playCustomerService.editPlyCustomerLockout(webUserInfo);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
    public void getPlyCustomerById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String customerIdStr = req.getParameter("uid");
        Integer uid = null;
        if (customerIdStr != null && !"".equals(customerIdStr)) {
            uid = Integer.parseInt(customerIdStr);
        }
        WebUserInfoServiceImpl playCustomerService = new WebUserInfoServiceImpl();
        WebUserInfo customer = playCustomerService.getPlyCustomerById(uid);
        ReturnResult rrs = null;
        if (customer != null) {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(),customer);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String pageStr = req.getParameter("page");
        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageSize = 10;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        String nickname = req.getParameter("nickname");
        String customerVipGradeStr = req.getParameter("vipGrade");
        String customerLoadGradeStr = req.getParameter("loadGrade");
        String lockoutStatusStr = req.getParameter("lockoutStatus");
        String godStatusStr = req.getParameter("godStatus");
        Integer godStatus = null;
        if (godStatusStr != null && !"".equals(godStatusStr)) {
            godStatus = Integer.parseInt(godStatusStr);
        }
        Integer lockoutStatus = null;
        if (lockoutStatusStr != null && !"".equals(lockoutStatusStr)) {
            lockoutStatus = Integer.parseInt(lockoutStatusStr);
        }
        Integer vipGrade = null;
        if (customerVipGradeStr != null && !"".equals(customerVipGradeStr)) {
            vipGrade = Integer.parseInt(customerVipGradeStr);
        }
        Integer loadGrade = null;
        if (customerLoadGradeStr != null && !"".equals(customerLoadGradeStr)) {
            loadGrade = Integer.parseInt(customerLoadGradeStr);
        }

        WebUserInfo inputCustomer = new WebUserInfo(nickname, vipGrade, loadGrade, null, godStatus, null, null, null, null, null, lockoutStatus, null);
        WebUserInfoServiceImpl playCustomerService = new WebUserInfoServiceImpl();
        Integer total = playCustomerService.countPlyCustomers(inputCustomer);
        //总页数计算公式 total%pageSize 能不能整除
        //能整除    total/pageSize   页
        //不能整除  total/pageSize+1  页
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        //检查当前要查的页数 是否超过了最大页数
        //如果超过了 则用最后一页查
        //修复bug时 有可能产生新bug
        //功能完成后 需要重新测试 之前测通的功能 可能会与现在的功能产生冲突 造成bug
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<WebUserInfo> customers = playCustomerService.listPlyCustomers(inputCustomer, page, pageSize);
        ReturnResult rrs = null;
        if (customers.size()>0) {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(),customers);
            PageInfo pageInfo = new PageInfo(page,pageSize,total);
            rrs.setPageInfo(pageInfo);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();


    }

}
